package com.sixth.volunteerservicesystem.Controller;

import com.sixth.volunteerservicesystem.Service.LoginService;
import com.sixth.volunteerservicesystem.domain.Admins;
import com.sixth.volunteerservicesystem.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.util.List;

@Controller
@RequestMapping("/login")
public class login {
    @Autowired
    private LoginService loginService;

    @RequestMapping("/")
    public String index(Model m) {
        return "login/login";
    }

    @RequestMapping("/getlogin")
    public String getlogin(String username, String password, HttpServletRequest request, HttpServletResponse response, Model m) throws Exception {
        System.out.println(username + " " + password);
        List<Users> list = loginService.getlogin(username, password);
        if (list != null) {
            request.getSession().setAttribute("id", list.get(0).getUserId());
            request.getSession().setAttribute("stat", "1");
            System.out.println(list.get(0).getNickNama());
            response.sendRedirect("/");
        } else {
            String msg = "账号或密码错误，请确认无误后再登录";
            m.addAttribute("msg", msg);
//            request.getRequestDispatcher("/login/").forward(request, response);
            return "/login/login";
        }
        return null;
    }
    @RequestMapping("/admins")
    public String indexadmin() {
        return "login/loginadmin";
    }

    @RequestMapping("/getloginadmin")
    public String getloginadmin(String username, String password, HttpServletRequest request, HttpServletResponse response, Model m) throws Exception {
        System.out.println(username + " " + password);
        List<Admins> list = loginService.getloginadmin(username, password);
        if (list != null) {
            request.getSession().setAttribute("id", list.get(0).getAdminId());
            request.getSession().setAttribute("stat", "1");
            System.out.println(list.get(0).getName());
            response.sendRedirect("/backend");
        } else {
            String msg = "账号或密码错误，请确认无误后再登录";
            m.addAttribute("msg", msg);
            return "/login/admins";
//            request.getRequestDispatcher("/login/").forward(request, response);
        }
        return null;
    }

    @RequestMapping("/out")
    @ResponseBody
    public void out(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().invalidate();
        response.sendRedirect("/");
    }

    @RequestMapping("/register")
    public String register() {
        return "login/register";
    }

    @RequestMapping("/getregister")
    public String getregister(HttpServletRequest request, HttpServletResponse response, Model m, String username, String password, String nickname) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("utf-8");
        String str = "你的名字不够响亮，早已被别人占用，换个更响亮的吧";
        String str1="该手机号已被注册过";
        Users users = new Users();
        List<Users> idlist = loginService.getuserbyid(username);
        List<Users> list = loginService.getuserbynickname(nickname);
        System.out.println(username);
        if (idlist == null) {
            if (list != null) {
                System.out.println(1);
                m.addAttribute("str", str);
//                request.getRequestDispatcher("/login/register").forward(request, response);
                return "/login/register";
            } else {
                System.out.println(2);
                users.setUserId(username);
                users.setPwd(password);
                users.setNickNama(nickname);
                loginService.insertuser(users);
                response.sendRedirect("/login/");
            }
        }else {
            m.addAttribute("str1", str1);
//            request.getRequestDispatcher("/login/register").forward(request, response);
            return "/login/register";
        }
        return null;
    }

    //test的out
    @RequestMapping("/test")
    public String test() {
        return "test/out";
    }
}
